package com.bh.extend.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.bh.common.annotation.Dict;
import com.bh.common.annotation.Excel;
import com.bh.common.constant.Constants;
import com.bh.common.core.domain.BaseEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.util.List;

/**
 * 客户单价对象 jc_customer_price
 *
 * @author slx
 * @date 2023-04-03
 */
@EqualsAndHashCode(callSuper = true)
@TableName("jc_customer_price")
@Data
public class JcCustomerPrice extends BaseEntity {

    

    /**
     * ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 客户ID
     */
    @Dict(dictTable = "jc_customer", dictLabel = "name", dictValue = "id")
    @Excel(name = "客户ID")
    private Long customerId;

    /**
     * 组织机构
     */
    @Excel(name = "组织机构")
    private Long jcOrganizationId;

    /**
     * 配送模式
     */
    @Dict(dictValue = "delivery_model")
    @Excel(name = "配送模式")
    private String model;
    @TableField(exist = false)
    private String[] models;

    /**
     * 出发网点
     */
    @Dict(dictTable = "jc_sys_organization", dictLabel = "name", dictValue = "id")
    @Excel(name = "出发网点")
    private Long fromOrg;
    @TableField(exist = false)
    private String fromOrgDtLabel;

    /**
     * 出发运点
     */
    @Excel(name = "出发运点")
    private Long fromServerZoneId;
    @TableField(exist = false)
    private String fromServerZoneIdDtLabel;

    /**
     * 目的网点
     */
    @Dict(dictTable = "jc_sys_organization", dictLabel = "name", dictValue = "id")
    @Excel(name = "目的网点")
    private Long toOrg;
    @TableField(exist = false)
    private String toOrgDtLabel;

    /**
     * 目的运点
     */
    @Excel(name = "目的运点")
    private Long toServerZoneId;
    @TableField(exist = false)
    private String toServerZoneIdDtLabel;


    /**
     * 收入价格(按数量)
     */
    @ApiModelProperty("收入价格(按数量)")
    @Excel(name = "收入价格(按数量)")
    private BigDecimal incomeNumPrice;


    /**
     * 收入价格(按数量)
     */
    @ApiModelProperty("收入价格(按重量)")
    @Excel(name = "收入价格(按重量)")
    private BigDecimal incomeWeightPrice;

    /**
     * 冷藏箱20升含以下
     */
    @ApiModelProperty("冷藏箱20升含以下")
    @Excel(name = "冷藏箱20升含以下")
    private BigDecimal incomeFreezer0;

    /**
     * 冷藏箱21升-60升以下
     */
    @ApiModelProperty("冷藏箱21升-60升以下")
    @Excel(name = "冷藏箱21升-60升以下")
    private BigDecimal incomeFreezer21;

    /**
     * 冷藏箱61升含以上
     */
    @ApiModelProperty("冷藏箱61升含以上")
    @Excel(name = "冷藏箱61升含以上")
    private BigDecimal incomeFreezer61;

    /**
     * 干线支出价格(按数量)
     */
    @Excel(name = "干线支出价格(按数量)")
    private BigDecimal trunkNumPrice;


    /**
     * 干线支出价格(按重量)
     */
    @Excel(name = "干线支出价格(按重量)")
    private BigDecimal trunkWeightPrice;

    /**
     * 冷藏箱20升含以下
     */
    @Excel(name = "市县冷藏箱20升含以下")
    private BigDecimal trunkFreezer0;

    /**
     * 冷藏箱21升-60升以下
     */
    @Excel(name = "市县冷藏箱21升-60升以下")
    private BigDecimal trunkFreezer21;

    /**
     * 冷藏箱61升含以上
     */
    @Excel(name = "市县冷藏箱61升含以上")
    private BigDecimal trunkFreezer61;

    /**
     * 支线配送价格(按数量)
     */
    @Excel(name = "支线配送价格(按数量)")
    private BigDecimal regionalNumPrice;
    
    
    /**
     * 支线配送价格(按重量)
     */
    @Excel(name = "支线配送价格(按重量)")
    private BigDecimal regionalWeightPrice;

    /**
     * 冷藏箱20升含以下
     */
    @Excel(name = "乡镇冷藏箱20升含以下")
    private BigDecimal regionalFreezer0;

    /**
     * 冷藏箱21升-60升以下
     */
    @Excel(name = "乡镇冷藏箱21升-60升以下")
    private BigDecimal regionalFreezer21;

    /**
     * 冷藏箱61升含以上
     */
    @Excel(name = "乡镇冷藏箱61升含以上")
    private BigDecimal regionalFreezer61;

    /**
     * 取货费
     */
    @ApiModelProperty("取货费")
    @Excel(name = "取货费")
    private BigDecimal pickupPrice;

    /**
     * 其他支出价格
     */
    @ApiModelProperty("其他支出价格")
    @Excel(name = "其他支出价格")
    private BigDecimal otherSpendPrice;
    /**
     * 是否有效
     */
    private String status;


    /** 分段体积价格信息 */
    @TableField(exist = false)
    private List<JcCustomerVolumePrice> jcCustomerVolumePriceList;


    /**
     * 获取取冷藏箱收入价格(按数量)
     *
     * @param packageType
     * @return
     */
    public BigDecimal getIncomeFreezerPrice(String packageType) {
        BigDecimal price = BigDecimal.ZERO;
        if ("21".equals(packageType)) {
            price = this.incomeFreezer0;
        } else if ("22".equals(packageType)) {
            price = this.incomeFreezer21;
        } else if ("23".equals(packageType)) {
            price = this.incomeFreezer61;
        }
        return price;
    }


    /**
     * 获取冷藏箱的支线配送价格(按数量)
     *
     * @param addressType
     * @param packageType
     * @return
     */
    public BigDecimal getTownOutcomeFreezerPrice(String addressType, String packageType) {
        BigDecimal price = BigDecimal.ZERO;
        // 如果是乡镇则取出送货费用
        if (Constants.ADDRESS_TYPE_VILLAGES.equals(addressType)) {
            if ("21".equals(packageType)) {
                price = this.regionalFreezer0;
            } else if ("22".equals(packageType)) {
                price = this.regionalFreezer21;
            } else if ("23".equals(packageType)) {
                price = this.regionalFreezer61;
            }
        }

        return price;
    }


    /**
     * 获取冷藏箱的干线价格
     * @param packageType
     * @return
     */
    public BigDecimal getTrunkOutcomeFreezerPrice(String packageType) {
        BigDecimal price = BigDecimal.ZERO;
        // 如果是乡镇则取出送货费用
        if ("21".equals(packageType)) {
            price = this.trunkFreezer0;
        } else if ("22".equals(packageType)) {
            price = this.trunkFreezer21;
        } else if ("23".equals(packageType)) {
            price = this.trunkFreezer61;
        }

        return price;
    }


    /**
     * 获取按体积收入价格(按数量)
     *
     * @param totalVolume
     * @return
     */
    public JcCustomerVolumePrice getVolumePrice(BigDecimal totalVolume, List<JcCustomerVolumePrice> customerVolumePrices) {
        // 从customerVolumePrices中根据totalWeight获取不同段的价格
        for (JcCustomerVolumePrice customerVolumePrice : customerVolumePrices) {
            if (totalVolume.compareTo(BigDecimal.valueOf(customerVolumePrice.getStartValue())) >= 0 && totalVolume.compareTo(BigDecimal.valueOf(customerVolumePrice.getEndValue())) < 0) {
                return customerVolumePrice;
            }
        }
        return null;
    }

}
